package com.resurvolution.kingcloudmusicspringboot.product.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.resurvolution.kingcloudmusicspringboot.common.utils.BaseContext;
import com.resurvolution.kingcloudmusicspringboot.product.dto.PlayListDTO;
import com.resurvolution.kingcloudmusicspringboot.product.entity.PlayListEntity;
import com.resurvolution.kingcloudmusicspringboot.product.entity.SongsEntity;
import com.resurvolution.kingcloudmusicspringboot.product.mapper.PlayListMapper;
import com.resurvolution.kingcloudmusicspringboot.product.mapper.SongsMapper;
import com.resurvolution.kingcloudmusicspringboot.product.service.PlayListService;
import com.resurvolution.kingcloudmusicspringboot.product.service.SongsService;
import com.resurvolution.kingcloudmusicspringboot.product.vo.PlayListVO;
import com.resurvolution.kingcloudmusicspringboot.product.vo.SongsVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class PlayListImpl extends ServiceImpl<PlayListMapper, PlayListEntity> implements PlayListService {

    @Resource
    private PlayListMapper playListMapper;

    @Resource
    private SongsService songsService;

    @Override
    public String insertPlayList(PlayListDTO playListDTO) {
        QueryWrapper<PlayListEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("song_id", playListDTO.getId());
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        if (playListMapper.selectOne(queryWrapper) == null) {
            PlayListEntity playListEntity = new PlayListEntity();
            playListEntity.setSongId(playListDTO.getId());
            playListEntity.setUserId(BaseContext.getCurrentId());
            playListEntity.setCreatedAt(LocalDateTime.now());
            playListMapper.insertOrUpdate(playListEntity);
            return "添加成功";
        }
        return "已更新播放列表";
    }

    @Override
    public List<PlayListVO> getPlayListByUserId(Integer userId) {
        QueryWrapper<PlayListEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());

        List<PlayListEntity> playListEntities = playListMapper.selectList(queryWrapper)
                .stream()
                .sorted(Comparator.comparing(PlayListEntity::getCreatedAt))
                .toList();

        return IntStream.range(0, playListEntities.size())
                .mapToObj(index -> {
                    PlayListEntity playListEntity = playListEntities.get(index);
                    PlayListVO playListVO = new PlayListVO();
                    SongsVO songsVO = new SongsVO();
                    BeanUtils.copyProperties(songsService.getSongById(playListEntity.getSongId()), songsVO);
                    playListVO.setSongVO(songsVO);
                    playListVO.setOrder(index + 1);
                    return playListVO;
                })
                .collect(Collectors.toList());
    }
}
